function bvqxff = bvqxfile_readformats(fdir)
% BVQXfile::<PRIVATE>::readformats  - reading BVQX formats from folder
%
% THIS FUNCTION IS AN INTERNAL FUNCTION OF THE CLASS
%
% @BVQXfile
%
% AND IT SHOULD NEVER BE CALLED FROM OUTSIDE THE CLASS CODE

% Version:  v0.7b
% Build:    7082923
% Date:     Aug-29 2007, 11:18 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% use global config
global bvqxfile_config;

% argument check
if nargin < 1 || ...
   ~ischar(fdir) || ...
    isempty(fdir) || ...
    exist(fdir(:)', 'dir') ~= 7
    error( ...
        'BVQXfile:Internal:BadArgument', ...
        'bvqx_read_formats requires one non-empty 1xN char folder argument.' ...
    );
end

% make valid folder name
fdir = fdir(:)';
if fdir(end) ~= filesep
    fdir(end + 1) = filesep;
end

% read format files
try
    bff = dir([fdir '*.bff']);
    tff = dir([fdir '*.tff']);

    % initialize global counter, extension list, and magic tokens array
    bnf = 0;
    tnf = 0;
    bffspec = struct;
    tffspec = struct;
    ex = struct;
    gx = [];
catch
    error( ...
        'BVQXfile:Internal:ErrorReadingDirContents', ...
        'Could not get contents of folder ''%s''.', ...
        fdir ...
    );
end

% init output argument
bvqxff = struct;

% parsing found BFF files
for fc = 1:numel(bff)

    % reading singular format
    try
        sf = bffparse([fdir bff(fc).name]);

        % store parsed info
        bnf = bnf + 1;
        if bnf == 1
            bffspec = sf;
            bffspec(numel(bff)).FFTYPE = 'BFF';
        else
            bffspec(bnf) = sf;
        end

        % add valid extensions -> singular format, and type config
        for ec = 1:length(sf.Extensions)
            lex = lower(sf.Extensions{ec});
            ex.(lex) = {bff(fc).name, bnf};
            bvqxfile_config.type.(lex) = struct;
            bvqxfile_config.update.(lex) = true;
        end

        % put magic tokens into main array
        for mc = 1:length(sf.Magic)
            if isempty(gx)
                gx = sf.Magic(mc);
            else
                gx(end+1) = sf.Magic(mc);
            end
        end
    catch
        warning( ...
            'BVQXfile:Internal:BadFileContents', ...
            'Bad file contents for ''%s'', please re-install.', ...
            bff(fc).name ...
        );
    end
end

% parsing found TFF files
for fc = 1:numel(tff)

    % reading singular format
    try
        sf = tffparse([fdir tff(fc).name]);

        % store parsed info
        tnf = tnf + 1;
        if tnf == 1
            tffspec = sf;
            tffspec(numel(tff)).FFTYPE = 'TFF';
        else
            tffspec(tnf) = sf;
        end

        % create empty settings struct for AFT
        bvqxfile_config.type.aft = struct;
        
        % add valid extensions -> singular format
        for ec = 1:length(sf.Extensions)
            lex = lower(sf.Extensions{ec});
            ex.(lex) = {tff(fc).name, tnf};
            bvqxfile_config.type.(lex) = struct;
            bvqxfile_config.update.(lex) = true;
        end

        % put magic tokens into main array
        for mc = 1:length(sf.Magic)
            if isempty(gx)
                gx = sf.Magic(mc);
            else
                gx(end+1) = sf.Magic(mc);
            end
        end
    catch
        warning( ...
            'BVQXfile:Internal:BadFileContents', ...
            'Bad file contents for ''%s'', please re-install.', ...
            tff(fc).name ...
        );
    end
end

% set output fields
bvqxff.bff = bffspec;
bvqxff.tff = tffspec;
bvqxff.extensions = ex;
bvqxff.magic = gx;
